package com.next.servlet;

import com.bean.Book;
import com.bean.BookPage;
import com.bean.UpdateBookNameHistory;
import com.dao.BookDao;
import com.dao.NextHistoryDao;
import com.utils.PageUtil;
import com.utils.ResponseUtils;
import com.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName BookServlet
 * @Desc 后台图书控制层
 * @Author xiaoding
 * @Date 2021/1/16 20:22
 * @Version 1.0
 */
@WebServlet(name = "NextBookServlet",value = "/NextBookServlet")

public class BookServlet extends HttpServlet {
    private static BookDao bookDao = new BookDao();
    private static NextHistoryDao nextHistoryDao = new NextHistoryDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        //判断功能模块
        if (str.equals("select")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有图书的数量
            int count = bookDao.bookCount();
            //定义访问的Servlet
            String targetUrl = "NextBookServlet";
            //定义参数
            String param = "flag=select";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<Book> book = bookDao.bookSelect(new BookPage(page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("book",book);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/index.jsp").forward(request, response);
        }else if (str.equals("search")){
            //获取图书名称
            String bName = request.getParameter("bName");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有图书的数量
            int count = bookDao.bookSearchCount("%" + bName + "%");
            //定义访问的Servlet
            String targetUrl = "NextBookServlet";
            //定义参数
            String param = "flag=search&bName="+bName;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<Book> book = bookDao.bookSearch(new BookPage("%" + bName + "%",page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("book",book);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/index.jsp").forward(request, response);
        }else if (str.equals("add")) {
            //获取参数
            String bNames = request.getParameter("bNames");
            String author = request.getParameter("author");
            int bCategory = Integer.parseInt(request.getParameter("bCategory"));
            String publishing = request.getParameter("publishing");
            int count = Integer.parseInt(request.getParameter("count"));
            double bookNumber = Double.parseDouble(request.getParameter("bookNumber"));

            //设置路径、状态
            String message = null;
            String url = null;

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //调用添加的方法
            int a = bookDao.addBook(new Book(bNames,author,bCategory,publishing,count,bookNumber));
            //判断是否添加成功
            if (a > 0) {
                message = "添加成功";
            }else {
                message = "添加失败";
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "NextBookServlet?flag=search&page="+page+"&bName="+bName;
            }else {
                url = "NextBookServlet?flag=select&page="+page;
            }
            //跳转路劲
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("update")) {
            //获取参数
            int bId = Integer.parseInt(request.getParameter("bId"));
            String bNames = request.getParameter("bNames");
            String bName2 = request.getParameter("bName2");
            String author = request.getParameter("author");
            int bCategory = Integer.parseInt(request.getParameter("bCategory"));
            String publishing = request.getParameter("publishing");
            int count = Integer.parseInt(request.getParameter("count"));
            double bookNumber = Double.parseDouble(request.getParameter("bookNumber"));

            //设置路径、状态
            String message = null;
            String url = null;

            //调用修改信息
            int a = 0;
            int b = 0;
            if (StringUtils.trimString(bName2).equals(StringUtils.trimString(bNames))) {
                a = bookDao.updateBook(new Book(bId,bNames,author,bCategory,publishing,count,bookNumber));

                //判断是否修改成功
                if (a > 0) {
                    message = "修改成功";
                }else {
                    message = "修改失败";
                }
            }else {
                a = bookDao.updateBook(new Book(bId,bNames,author,bCategory,publishing,count,bookNumber));
                b = nextHistoryDao.updateBookNameHistory(new UpdateBookNameHistory(bId,bNames));

                //判断是否修改成功
                if (a > 0) {
                    message = "修改成功";
                }else {
                    message = "修改失败";
                }
            }


            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "NextBookServlet?flag=search&page="+page+"&bName="+bName;
            }else {
                url = "NextBookServlet?flag=select&page="+page;
            }
            //跳转路劲
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("delete")) {
            //获取图书Id
            int bId = Integer.parseInt(request.getParameter("bId"));

            //设置路径、状态
            String message = null;
            String url = null;

            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //调用删除的方法
            int a = nextHistoryDao.deleteBookIdHistory(bId);
            int b = bookDao.deleteBook(bId);


            //判断是否删除成功
            if (a > 0 && b > 0) {
                message = "删除成功";
            }else {
                message = "删除成功";
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "NextBookServlet?flag=search&page="+page+"&bName="+bName;
            }else {
                url = "NextBookServlet?flag=select&page="+page;
            }
            //跳转路劲
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("selectId")) {
            //获取图书Id、分页、搜索名称
            int bId = Integer.parseInt(request.getParameter("bId"));
            //分页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
            }

            //根据图书Id，查询图书信息
            Book book = bookDao.bookSelectId(bId);

            //设置会话
            request.setAttribute("book",book);
            //跳转方法
            request.getRequestDispatcher("next/updateBook.jsp?page="+page+"&bName="+bName).forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
